<?php
/**
 * Object representing a feedback
 *
 * @package Concerto
 * @subpackage Models
 * @author Jason Raede <jason@torchdm.com>
 */
namespace application\models;

class Feedback {
	
	/**
	 * @var string
	 */
	protected $feedback_comment;
	
	/** 
	 * @var int
	 */
	protected $feedback_rating;
	
	/**
	 * @var int
	 */
	protected $feedback_user;
	
	/**
	 * @var string
	 */
	protected $feedback_timestamp;
	
	/**
	 * @var string
	 */
	protected $feedback_display_name;
	
	/**
	 * @var int
	 */
	protected $feedback_content;
	
	/**
	 * @var int
	 */
	protected $feedback_id;
	
	
	/**
	 * If $id is provided, initialize the object with its database data
	 * 
	 * @param int $id
	 */
	public function __construct($id = null) {
		if($id) {
			$row = DB()->getRow("SELECT * FROM `cn_feedback` WHERE `feedback_id`='$id'");
			$this->feedback_comment = $row->feedback_comment;
			$this->feedback_rating = $row->feedback_rating;
			$this->feedback_user = $row->feedback_user;
			$this->feedback_timestamp = $row->feedback_timestamp;
			$this->feedback_display_name = $row->feedback_display_name;
			$this->feedback_content = $row->feedback_content;
			$this->feedback_id = $id;
		}
	}
	
	/**
	 * Insert or update database row, according to feedback settings on the content type
	 *
	 * @return array DBObject status array.
	 */ 
	public function save() {
		global $concerto;
		// Grab all data from request
		$this->feedback_comment = $_REQUEST['feedback_comment'];
		$this->feedback_rating = $_REQUEST['feedback_rating'];
		$this->feedback_user = ($_REQUEST['feedback_user']) ? $_REQUEST['feedback_user'] : (($concerto->User) ? $concerto->User->db_fields['user_id'] : false);
		$this->feedback_content = $_REQUEST['feedback_content'];
		$this->feedback_id = $_REQUEST['feedback_id'];
		
		$dbFields = array(
			'feedback_comment'=>$this->feedback_comment,
			'feedback_rating'=>$this->feedback_rating,
			'feedback_user'=>$this->feedback_user,
			'feedback_content'=>$this->feedback_content,
			'feedback_id'=>$this->feedback_id,
			'feedback_ip'=>$_SERVER['REMOTE_ADDR']
		);
		
		// Check permissions based on feedback configurations for the content type
		$type = DB()->getVar("SELECT `content_type` FROM `cn_content` WHERE `content_id`='$this->feedback_content'");
		
		$type = new \application\models\ContentType($type);
		
		switch($type->getFeedbackMerge()) {
			case 'together_req':
				if(!$this->feedback_comment || !$this->feedback_rating) {
					return array('status'=>'error', 'error'=>'You must provide both a comment and a rating.');
				}
				break;
			default:
			case 'together_noreq':
				if(!$this->feedback_comment && !$this->feedback_rating) {
					return array('status'=>'error', 'error'=>'You must provide either a comment or a rating.');
				}
				break;	
			
			case 'separate':
				if($this->feedback_comment && $this->feedback_rating) {
					return array('status'=>'error', 'errors'=>array('You can only provide either a comment or a rating.'));
				}
				break;
		}
		
		// Check if they are limited to posting only one
		if($this->feedback_comment && $type->commentsLimit) {
			if($this->feedback_user) {
				$count = DB()->getCount("SELECT `feedback_id` FROM `cn_feedback` WHERE `feedback_content`='$this->feedback_content' AND `feedback_comment` != '' AND `feedback_user`='$this->feedback_user'");
			}
			else {
				$count = DB()->getCount("SELECT `feedback_id` FROM `cn_feedback` WHERE `feedback_content`='$this->feedback_content' AND `feedback_comment` != '' AND `feedback_ip`='".$_SERVER['REMOTE_ADDR']."'");
			}
			
			if($count) {
				return array('status'=>'error', 'errors'=>array('You are only permitted one comment.'));
			}
		}
		
		if($this->feedback_rating && $type->getRatingsLimit()) {
			if($this->feedback_user) {
				$count = DB()->getCount("SELECT `feedback_id` FROM `cn_feedback` WHERE `feedback_content`='$this->feedback_content' AND `feedback_rating` != '' AND `feedback_user`='$this->feedback_user'");
			}
			else {
				$count = DB()->getCount("SELECT `feedback_id` FROM `cn_feedback` WHERE `feedback_content`='$this->feedback_content' AND `feedback_rating` != '' AND `feedback_ip`='".$_SERVER['REMOTE_ADDR']."'");
			}
			
			if($count) {
				return array('status'=>'error', 'errors'=>array('You are only permitted one rating.'));
			}
		}
		
		if($this->feedback_id) {
			DB()->update('cn_feedback', 'feedback_id', $this->feedback_id, $dbFields);
			return array('status'=>'success', 'id'=>$this->feedback_id);
		}
		else {
			DB()->insert('cn_feedback', $dbFields);
			$id = DB()->lastInsert();
			return array('status'=>'success', 'id'=>$id);
		}
		
	}
	
	/**
	 * Returns the rating
	 *
	 * @return int
	 */
	public function feedbackRating() {
		return $this->feedback_rating;
	}
	
	/**
	 * Returns the comment
	 *
	 * @return string
	 */
	public function feedbackComment() {
		return $this->feedback_comment;
	}
	
	/**
	 * Returns the timestamp according to the format
	 *
	 * @param string $format
	 * @return string
	 */
	public function feedbackTime($format) {
		return date($format, strtotime($this->feedback_timestamp));
	}
	
	/**
	 * Return either the \application\models\User object, or the display name
	 *
	 * @return mixed
	 */
	public function feedbackUser() {
		if($this->feedback_user) {
			// Get display name of author
			$user = new User($this->feedback_user);
			$return = $user->displayName();
			unset($user);
		}
		else {
			$return = $this->feedback_display_name;
		}
		return $return;
	}
	
	/**
	 * Delete the feedback from the database
	 */
	public function delete() {
		DB()->query("DELETE FROM `cn_feedback` WHERE `feedback_id`='$this->feedback_id'");
	}
}

?>